package com.bugsnag.ohos;

import java.io.IOException;

/**
 * Represents a single native stackframe
 *
 * @since 2021-07-01
 */
public class NativeStackframe implements JsonStream.Streamable {
    /**
     * The name of the method that was being executed
     */
    String method;

    /**
     * The location of the source file
     */
    String file;

    /**
     * The line number within the source file this stackframe refers to
     */
    Number lineNumber;

    /**
     * The address of the instruction where the event occurred.
     */
    long frameAddress;

    /**
     * The address of the function where the event occurred.
     */
    long symbolAddress;

    /**
     * The address of the library where the event occurred.
     */
    long loadAddress;

    /**
     * The type of the error
     */
    ErrorType type = ErrorType.C;

    /**
     * constructor
     *
     * @param method
     * @param file
     * @param lineNumber
     * @param frameAddress
     * @param symbolAddress
     * @param loadAddress
     */
    public NativeStackframe(String method, String file, Number lineNumber,
                            long frameAddress, long symbolAddress, long loadAddress) {
        this.method = method;
        this.file = file;
        this.lineNumber = lineNumber;
        this.frameAddress = frameAddress;
        this.symbolAddress = symbolAddress;
        this.loadAddress = loadAddress;
    }

    @Override
    public void toStream(JsonStream writer) throws IOException {
        writer.beginObject();
        writer.name("method").value(method);
        writer.name("file").value(file);
        writer.name("lineNumber").value(lineNumber);
        writer.name("frameAddress").value(frameAddress);
        writer.name("symbolAddress").value(symbolAddress);
        writer.name("loadAddress").value(loadAddress);
        if (type != null) {
            writer.name("type").value(type.desc);
        }
        writer.endObject();
    }
}
